#ifndef VSALLOC_H
#define VSALLOC_H
#include "VSMemManager.h"
#ifdef VSDATESTRUCT_EXPORTS
#define VSDATESTRUCT_API __declspec(dllexport) 
#else 
#define VSDATESTRUCT_API __declspec(dllimport) 
#endif
namespace VSEngine2
{
	template <class T,VSMemManagerFun MMFun = VSMemObject::GetMemManager>
	class VSContainer : public VSMemObject
	{
	protected:
		T * New(unsigned int uiNum)
		{
			if (!uiNum)
			{
				return NULL;
			}
#ifdef USE_CUSTOM_NEW
			T* pPtr = (T *)MMFun().Allocate(uiNum * sizeof(T),0,true);
			VSMAC_ASSERT(pPtr);
			if (!pPtr)
			{
				return NULL;
			}
			if (ValueBase<T>::NeedsConstructor)
			{
				for (unsigned int i = 0 ; i < uiNum ; i++)
				{
					VS_NEW(pPtr + i)T();
				}
			}
#else
			T* pPtr = VS_NEW T[uiNum];
			VSMAC_ASSERT(pPtr);
			if (!pPtr)
			{
				return NULL;
			}
#endif
			return pPtr;
		}

		void Delete(T * & pPtr,unsigned int uiNum)
		{
#ifdef USE_CUSTOM_NEW
			if (uiNum > 0 && pPtr)
			{
				if (ValueBase<T>::NeedsDestructor)
				{
					for (unsigned int i = 0 ; i < uiNum ; i++)
					{
						(pPtr + i)->~T();
					}
				}
			}
			else
			{
				return;
			}

			MMFun().Deallocate((char *)pPtr,0,true);
			pPtr = NULL;
#else
			VSMAC_DELETEA(pPtr);
#endif
			
		}

	};
}

#endif